package br.ufrn.engcomp.domain;

public class ParserImpl implements Parser {

	private AcquisitionBoard acqBoard = new MultQ();
	private int channel;
	private double value;

	public String parse(String cmd) throws AcquisitonBoardException {

		if (cmd.trim().equals("HELLO")){
			acqBoard.initBoard();
			return "HI";
		}
		else if (cmd.trim().equals("RESET")) {
			for (int i = 0; i < 15; i++) {
				acqBoard.writeDA(i, 0.0);
			}
			return "ACK";
		}
		// WRITE [CHANNEL] [VALUE]
		else if (cmd.contains("WRITE")) {
			cmd = cmd.replace("WRITE ", "");
			String[] dois = cmd.split(" ");
			channel = Integer.valueOf(dois[0]);
			value = Double.valueOf(dois[1]);
			acqBoard.writeDA(channel, value);
			return "ACK";
		}
		// READ [CHANNEL]
		else if (cmd.contains("READ")) {
			cmd = cmd.replace("READ", "");
			cmd = cmd.trim();
			channel = Integer.valueOf(cmd);
			value = acqBoard.readAD(channel);
			return String.valueOf(value);
		}
		
		return "INVALID COMMAND";
	}

}
